//
//  MineTableViewController.m
//  Car
//
//  Created by lanou on 15/9/17.
//  Copyright (c) 2015年 朱鸿儒. All rights reserved.
//

#import "MineTableViewController.h"
#import "FuctionTableViewCell.h"
#import "ViewOfHeader.h"
#import "EnterViewController.h"
#import "FavoriteCollectionViewController.h"
//#import "HistoryTableViewController.h"
#import "HistoryViewController.h"
@interface MineTableViewController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIAlertViewDelegate>

@property (nonatomic, strong) ViewOfHeader *headerView;

@property (nonatomic, strong) NSMutableArray *introduceArray;

@property (nonatomic, strong) NSString *filePath;

//  调用闪光灯时候创建的类
@property (nonatomic, strong) AVCaptureSession *AVSession;

@end

@implementation MineTableViewController





- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    
    [self setUpHeaderViewAndNavicationBar];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setUpHeaderViewAndNavicationBar) name:@"myPhotoAndName" object:nil];
    
    
    //  注册
    [self.tableView registerClass:[FuctionTableViewCell class] forCellReuseIdentifier:@"tm"];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



#pragma mark  --- 设置导航条和headerView ---

- (void)setUpHeaderViewAndNavicationBar {
    
    NSString *name = @"111";
    
    self.headerView = [[ViewOfHeader alloc] initWithFrame:CGRectMake(0, 0, kWidth, 180) name:name];
    self.navigationController.navigationBar.translucent = YES;
    
    EnterInformationHandle *handle = [EnterInformationHandle share];
    if (handle.enterOrNot == YES) {
        
        [self.headerView.nameAndRegisterButton setTitle:handle.myName forState:UIControlStateNormal];
        self.headerView.nameAndRegisterButton.titleLabel.font = [UIFont fontWithName:@"AppleGothic" size:17];
        self.headerView.nameAndRegisterButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        self.headerView.nameAndRegisterButton.contentEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
        
        //  取出本地的信息
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSArray *array = [user objectForKey:@"911"];
        
        //  沙盒
        NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
        NSString *documentsPath = [docPath objectAtIndex:0];
        NSString *imagePath = [documentsPath stringByAppendingPathComponent:@"myP.png"];
        
        NSData *data = [NSData dataWithContentsOfFile:imagePath];
        UIImage *image = [[UIImage alloc] initWithData:data];
        
        if (image != nil) {
            [self.headerView.photoButton setBackgroundImage:image forState:UIControlStateNormal];
        }
        
        [self.headerView.nameAndRegisterButton setTitle:array[2] forState:UIControlStateNormal];
        [self.headerView.nameAndRegisterButton addTarget:self action:@selector(logout) forControlEvents:UIControlEventTouchUpInside];
        
        //  设置头像
        [self.headerView.photoButton addTarget:self action:@selector(changePhoto) forControlEvents:UIControlEventTouchUpInside];

        
        
        NSLog(@"我变啦");
    }
    else {      //  handle.enterOrNot == NO
        
        [self.headerView.nameAndRegisterButton setTitle:@"点击登录/注册" forState:UIControlStateNormal];
        self.headerView.nameAndRegisterButton.titleLabel.font = [UIFont fontWithName:@"AmericanTypewriter-Bold" size:17];
        
        [self.headerView.nameAndRegisterButton addTarget:self action:@selector(actionEnterOrRegister) forControlEvents:UIControlEventTouchUpInside];
        [self.headerView.photoButton addTarget:self action:@selector(actionEnterOrRegister) forControlEvents:UIControlEventTouchUpInside];
        

        NSLog(@"登录");
        
        
    }
    
    
        self.tableView.tableHeaderView = self.headerView;
}



#pragma mark ---   注销

- (void)logout {
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"通知" message:@"确定要注销吗" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    
    [alertView show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (buttonIndex == 1) {
        
        EnterInformationHandle *handle = [EnterInformationHandle share];
        handle.enterOrNot = NO;
        
        NSUserDefaults *user911 = [NSUserDefaults standardUserDefaults];
        [user911 removeObjectForKey:@"911"];
        [user911 synchronize];
        
        
        [self actionEnterOrRegister];

    }
    
    
}



- (void)changePhoto {
    
    UIActionSheet *mySheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相册", @"相机", nil];
    [mySheet showFromTabBar:self.tabBarController.tabBar];
    
}



#pragma mark ---   上拉菜单 照片

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    //  注意：打开相册的iphone和ipad不一样 这里是iphone的
    if (buttonIndex == 0) {
        UIImagePickerController *pickImage = [[UIImagePickerController alloc] init];
        
        pickImage.delegate = self;
        pickImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        
        pickImage.allowsEditing = YES;
        
        [self presentViewController:pickImage animated:YES completion:^{
            
            
            
        }];
        
    }
    //  相机
    if (buttonIndex == 1) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            
//            UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
            
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            picker.allowsEditing = YES;
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            
            [self presentViewController:picker animated:YES completion:nil];
            
            //判断是否可以打开相机,模拟器无法使用此功能
//                UIImagePickerController *picker = [[UIImagePickerController alloc] init];
//                picker.delegate = self;
//                picker.allowsEditing = YES; //是否可编辑
//                //摄像头
//                picker.sourceType = UIImagePickerControllerSourceTypeCamera;
//                [self presentViewController:picker animated:YES completion:nil];

            
        }
        else {
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"你没有摄像头" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            
            [alert show];
            
        }
        
    }
}




- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
        
        // 判断获取类型：图片
        
        if ([mediaType isEqualToString:( NSString *)kUTTypeImage]){
            
            UIImage *theImage = nil;
            
            // 判断，图片是否允许修改
            
            if ([picker allowsEditing]){
                
                //获取用户编辑之后的图像
                
                theImage = [info objectForKey:UIImagePickerControllerEditedImage];
                
            } else {
                
                // 照片的元数据参数
                
                theImage = [info objectForKey:UIImagePickerControllerOriginalImage];
                
            }
            
            [self.headerView.photoButton setBackgroundImage:theImage forState:UIControlStateNormal];
            
            //  把图片转化成data
            NSData *imageData = UIImagePNGRepresentation(theImage);
            
            //  把头像图片 存入沙盒 持久化
            NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsPath = [docPath objectAtIndex:0];
            NSString *imagePath = [documentsPath stringByAppendingPathComponent:@"myP.png"];
            
            
            [imageData writeToFile:imagePath atomically:YES];
            
            
        }
        
        [picker dismissViewControllerAnimated:YES completion:^{
            
        }];

    }
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        
        //  得到图片
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        //  存入相册
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
        
        [self dismissViewControllerAnimated:YES completion:nil];
        
    }
    
    
}




- (void)actionEnterOrRegister {
    
    EnterViewController *enterVC = [[EnterViewController alloc] init];
    
    //  隐藏tabBar
    self.tabBarController.tabBar.hidden = YES;
    
    [self.navigationController pushViewController:enterVC animated:YES];
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    switch (section) {
        case 0:
            return 6;
            break;
            
        case 1:
            return 1;
            break;
            
        default:
            return 0;
            break;
    }

    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FuctionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tm" forIndexPath:indexPath];
    //   cell右边有个小箭头的样式
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    self.introduceArray = [NSMutableArray arrayWithObjects:@"我的收藏",@"浏览历史",@"我的主帖",@"我的口碑",@"我的好友",@"草稿箱", nil];
    
    if (indexPath.section == 0) {
        
    if (indexPath.row == 0) {
        cell.textLabel.text = self.introduceArray[indexPath.row];
        cell.imageView.image = [UIImage imageNamed:@"shoucang"];

    }
    if (indexPath.row == 1) {
        cell.textLabel.text = self.introduceArray[indexPath.row];
        cell.imageView.image = [UIImage imageNamed:@"lishi"];

    }
    if (indexPath.row == 2) {
        cell.textLabel.text = self.introduceArray[indexPath.row];
        cell.imageView.image = [UIImage imageNamed:@"zhutie"];

    }
    if (indexPath.row == 3) {
        cell.textLabel.text = self.introduceArray[indexPath.row];
        cell.imageView.image = [UIImage imageNamed:@"koubei"];

    }
    if (indexPath.row == 4) {
        cell.textLabel.text = self.introduceArray[indexPath.row];
        cell.imageView.image = [UIImage imageNamed:@"haoou"];

    }
    if (indexPath.row == 5) {
        cell.textLabel.text = self.introduceArray[indexPath.row];
        cell.imageView.image = [UIImage imageNamed:@"caogao"];

    }
        
}
    else {
        cell.textLabel.text = @"设置";
        cell.imageView.image = [UIImage imageNamed:@"shezhi"];
        
        
    }
    
    return cell;
}


#pragma mark ---  选中cell的点击事件

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([EnterInformationHandle share].enterOrNot == YES) {
        if (indexPath.section == 0) {
            if (indexPath.row == 0) {
                FavoriteCollectionViewController *favo = [[FavoriteCollectionViewController alloc]init];
                [self.navigationController pushViewController:favo animated:YES];
			}else if (indexPath.row == 1){
				HistoryViewController *his = [[HistoryViewController alloc] init];
				[self.navigationController pushViewController:his animated:YES];
			
			}
            else{
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"该功能暂未开放" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
                [alert show];
            }
        }
    }
    else if(indexPath.section == 0){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"登录后体验更加丰富" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
    }
    if (indexPath.section == 1) {
        //  进入 ->设置
        [self.navigationController pushViewController:[Setting shareWithSetting].setVC animated:YES];
    }

}



#pragma mark ---   移动cell

@end
